//
//  CustomBtn.swift
//  whale
//
//  Created by topnet on 2022/3/8.
//

import UIKit
enum CustomBtnMode {
    ///文字上 图片下
    case top
    ///文字下 图片上
    case bottom
    ///文字左 图片右
    case left
    ///文字右 图片左
    case right
}
class CustomBtn: UIControl {
    
    var title: String? {
        didSet{
            if lbl.text != title {
                lbl.text = title
                setNeedsLayout()
                layoutIfNeeded()
            }
            
        }
    }
    
    var img: UIImage? {
        didSet {
            if iv.image != img {
                iv.image = img
                setNeedsLayout()
                layoutIfNeeded()
            }
        }
    }
    
    var selectedTitle: String?
    
    var selectedImg: UIImage?
    
    var textNormalColor: UIColor? = .title
    var textSelectedColor: UIColor? = ._blue
    var textNormalFont: UIFont? = .font(13)
    var textSelectedFont: UIFont?
    
    override var isSelected: Bool {
        didSet {
            if isSelected {
                iv.image = selectedImg ?? img
                lbl.text = selectedTitle ?? title
                lbl.textColor = textSelectedColor
                lbl.font = textSelectedFont ?? textNormalFont
            }else {
                iv.image = img
                lbl.text = title
                lbl.textColor = textNormalColor
                lbl.font = textNormalFont
            }
            setNeedsLayout()
            layoutIfNeeded()
        }
    }
    
    var spacing: CGFloat = 5
    var ivSize: CGSize = .zero

    var mode: CustomBtnMode = .left
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if iv.image == nil || lbl.text == nil || bounds.size.equalTo(.zero) {
            return
        }
        let _ivSize = ivSize == .zero ? (iv.image?.size ?? .zero) : ivSize
        let _textSize = lbl.text!.size(font: lbl.font, maxSize: CGSize(width: bounds.width-_ivSize.width-spacing, height: 16.adapter))
        
        switch mode {
        case .left:
            let _left = (bounds.width - _ivSize.width - spacing - _textSize.width)/2
            iv.frame = CGRect(x: _left, y: (bounds.height-_ivSize.height)/2, width: _ivSize.width, height: _ivSize.height)
            lbl.frame = CGRect(x: iv.frame.maxX+spacing, y: 0, width: _textSize.width, height: bounds.height)
        case .right:
            let _left = (bounds.width - _ivSize.width - spacing - _textSize.width)/2
            lbl.frame = CGRect(x: _left, y: 0, width: _textSize.width, height: bounds.height)
            iv.frame = CGRect(x: lbl.frame.maxX+spacing, y: (bounds.height-_ivSize.height)/2, width: _ivSize.width, height: _ivSize.height)
        case .top:
            let _top = (bounds.height - _ivSize.height - spacing - _textSize.height)/2
            lbl.frame = CGRect(x: 0, y: _top, width: bounds.width, height: _textSize.height)
            iv.frame = CGRect(x: (bounds.width-_ivSize.width)/2, y: lbl.frame.maxY+spacing, width: _ivSize.width, height: _ivSize.height)
        case .bottom:
            let _top = (bounds.height - _ivSize.height - spacing - _textSize.height)/2
            iv.frame = CGRect(x: (bounds.width-_ivSize.width)/2, y: _top, width: _ivSize.width, height: _ivSize.height)
            lbl.frame = CGRect(x: 0, y: iv.frame.maxY+spacing, width: bounds.width, height: _textSize.height)
        }
    }
    
    lazy var iv: UIImageView = {
        let iv = UIImageView()
        addSubview(iv)
        return iv
    }()
    
    lazy var lbl: UILabel = {
        let lbl = UILabel()
        lbl.textAlignment = .center
        lbl.font = textNormalFont
        lbl.textColor = textNormalColor
        addSubview(lbl)
        return lbl
    }()
}

